﻿//=================================================== 
// 
// 		All rights reserved  
// 文件名 :SmsAdapters 
// 简介: 
// 作者： 小钟
// 日期：2009-7-10 11:13:59 
// 
//====================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GCELibrary.Current.SmsMessage
{
    #region 接口
    /// <summary>
    /// 短信接口适配器
    /// </summary>
    public interface ISmsAdpter
    {
        bool IsConnect { get; }
        bool Connect();
        void Disonnect();
        bool SendMsg(string msgContet, string receivNum);
        int GetCount();
    }
    #endregion

    #region 短信易平台
    public class MobsetAdpter : ISmsAdpter
    {
        #region 参数配置

        string _strServer { get { return "www.mobset.com"; } }
        int _companyID { get { return 110050; } }
        string _userName = "Admin";
        string _password="579922";
        int _lTime { get { return 60; } }

        #endregion

        private bool _isConnect = false;

        public MobsetAdpter()
        {
            var smsSet = System.Configuration.ConfigurationSettings.AppSettings["sms"];
            if (smsSet != null)
            {
                if (smsSet.Split('|').Length >= 2)
                {
                    _userName = smsSet.Split('|')[0];
                    _password = smsSet.Split('|')[1];
                }
            }
            if (!this._isConnect)//没有连接服务器才进行连接
            {
                ISmsAdpter sms = this;
                sms.Connect();
            }
        }
        #region ISmsAdpter 成员
        /// <summary>
        ///  是否连接
        /// </summary>
        bool ISmsAdpter.IsConnect { get { return _isConnect; } }

        /// <summary>
        /// 连接
        /// </summary>
        /// <param name="strServer">服务器</param>
        /// <param name="companyID">企业ID</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <param name="lTime">超时时间</param>
        /// <returns>是否成功</returns>
        private bool ConnectToSms(string strServer, int companyID, string userName, string password, int lTime)
        {
            IntPtr intPtr = IntPtr.Zero;
            int returnValue = SmsImportDll.Sms_Connect(strServer, companyID, userName, password, lTime, intPtr);
            switch (returnValue)
            {
                case 0:
                    return true;
                case -1:
                    return false;
                case -2:
                    return false;
                case -3:
                    return false;
                case -4:
                    return false;
                default:
                    return false;
            }
        }

        #region 服务器断开和连接
        /// <summary>
        /// 连接服务器 默认超时60秒
        /// </summary>
        /// <returns></returns>
        bool ISmsAdpter.Connect()
        {
            return ConnectToSms(_strServer, _companyID, _userName, _password, _lTime);
        }

        /// <summary>
        /// 断开连接
        /// </summary>
        void ISmsAdpter.Disonnect()
        {
            SmsImportDll.Sms_DisConnect();//断开连接
        }
        #endregion

        #region 发送短信
        bool ISmsAdpter.SendMsg(string msgContet, string receivNum)
        {

            int msgID;//短信ID初始值

            int returnValue = 0;
            //调用DLL接口方法发送短信
            returnValue = SmsImportDll.Sms_Send(receivNum, msgContet, out msgID);
            //根据返回信息判断是否发送成功
            if (returnValue >= 0) { return true; }
            else { return false; }
        }

        #endregion


        #region 获取短信条数

        int ISmsAdpter.GetCount()
        {
            int count = 0;

            int returnValue = SmsImportDll.Sms_KYSms(); //获取方法返回
            if (returnValue >= 0)
            {
                count = returnValue;
            }

            return count;
        }


        #endregion

        #endregion
    }
    #endregion
}
